package io.renren.modules.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.GoogleAuthenticatorUtils;
import io.renren.common.utils.Result;
import io.renren.modules.pay.dao.GoogleauthDao;
import io.renren.modules.pay.dto.GoogleauthDTO;
import io.renren.modules.pay.entity.GoogleauthEntity;
import io.renren.modules.pay.service.GoogleauthService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.enums.SuperAdminEnum;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 谷歌验证
 *
 * @author ms 
 * @since 1.0.0 2019-06-24
 */
@Service
public class GoogleauthServiceImpl extends CrudServiceImpl<GoogleauthDao, GoogleauthEntity, GoogleauthDTO> implements GoogleauthService {

	@Autowired
	private GoogleauthDao googleauthDao;
    @Override
    public QueryWrapper<GoogleauthEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<GoogleauthEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

	@Override
	public Result<Object> createGoogleAuthQRCodeData(String service) {
		UserDetail user = SecurityUser.getUser();
		//获取用户名称(从数据库或者缓存),可以是登录名,邮箱,手机(不参与运算,只是为了与其他服务作区分)
		String account =user.getUsername();
		GoogleauthEntity selectOne = googleauthDao.selectOne(new QueryWrapper<GoogleauthEntity>().eq("userid", user.getId()).eq("servicename", service));
		Map<String,Object> map = new HashMap<>();
		String url ="";
		if(selectOne==null) {
			String secretKey = GoogleAuthenticatorUtils.createSecretKey();
			GoogleauthEntity google  = new GoogleauthEntity();
			google.setSecretkey(secretKey);
			google.setUserid(user.getId());
			google.setUsername(account);
			google.setServicename(service);
			google.setBindstatus(1);
			googleauthDao.insert(google);
			url = GoogleAuthenticatorUtils.getQRBarcodeURL(account, service, secretKey);
			map.put("url",url);
			return  new Result<>().ok(map);
		}
		if(selectOne.getBindstatus()==1) {
			 url = GoogleAuthenticatorUtils.getQRBarcodeURL(selectOne.getUsername(), selectOne.getServicename(), selectOne.getSecretkey());
			 map.put("url", url);
			 return new Result<>().ok(map);
		}
		map.put("url", false);
		return new Result<>().ok(map);
	}

	@Override
	public Result<Object> verify(Map<String, Object> parmas) {
		Long userid = Long.valueOf( parmas.get("userid").toString());
		GoogleauthEntity entity = googleauthDao.selectOne(new QueryWrapper<GoogleauthEntity>().eq("userid", userid).eq("servicename", parmas.get("service").toString()));
		if(entity==null) {
			return new Result<>().error("未绑定goole令牌");
		}
		if(entity.getBindstatus()==1) {
			entity.setBindstatus(0);//绑定成功
			entity.setUpdatetime(new Date());
			googleauthDao.updateById(entity);
		}
		boolean verify = GoogleAuthenticatorUtils.verify(entity.getSecretkey(), parmas.get("code").toString());
		if(verify) {
			return new Result<>().ok();
		}
		return new Result<>().error("口令错误");
		
	}

	@Override
	public GoogleauthEntity selectByUserId(Long id) {
		return googleauthDao.selectOne(new QueryWrapper<GoogleauthEntity>().eq("userid", id).eq("servicename", "verify"));
	}
}